﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WINADM.Data;
using log4net;
using System.Diagnostics;

namespace WINADM.Server
{
    public class PerformanceMonitor : IPerformanceMonitor
    {
        Computer _computer;

        public Computer Computer
        {
            get { return _computer; }
            set { _computer = value; }
        }
        ILog _logger;
        PerformanceCounter ramCounter;
        PerformanceCounter cpuCounter;
        ISystemInfo _systemInfo;

        public PerformanceMonitor(Computer computer,ILog logger,ISystemInfo systemInfo)
        {
            this._computer = computer;
            this._logger = logger;
            this._systemInfo = systemInfo;
            ramCounter = new PerformanceCounter("Memory", "Available KBytes");
            cpuCounter = new PerformanceCounter();
            cpuCounter.CategoryName = "Processor";
            cpuCounter.CounterName = "% Processor Time";
            cpuCounter.InstanceName = "_Total";
        }

        public CPUUsage UpdateCPUUsageData()
        {
            CPUUsage cpuUsage = new CPUUsage();
            cpuUsage.ComputerId = Computer.Id;
            cpuUsage.Time = _systemInfo.TimeStamp;
            cpuUsage.Value =(Byte) Math.Round(cpuCounter.NextValue());
            return cpuUsage;
        }

        public RAMUsage UpdateRAMUsageData()
        {
            RAMUsage ramUsage = new RAMUsage();
            ramUsage.ComputerId = Computer.Id;
            ramUsage.Time = _systemInfo.TimeStamp;
            ramUsage.Value = (int)Math.Round(ramCounter.NextValue());
            return ramUsage;
        }
    }
}
